/*
 * @Author: happyBoomL
 * @Date: 2025-11-08 16:31:41
 * @LastEditors: happyBoomL
 * @LastEditTime: 2025-11-08 17:37:52
 * @Description:  自定义404错误类
 */
class NotFoundError extends Error {
    constructor(message) {
        super(message);
        this.name = "NotFoundError";
    }
}

/**
 * @description: 成功响应
 * @param {Object} res express的响应对象
 * @param {String} message 消息
 * @param {Object} data 数据
 * @return {*}
 */
function success(res, message, data = {}, code = 200) {
    res.status(code).json({
        status: true,
        message: message,
        data: data
    })
}
/**
 * @description: 失败响应
 * @param {*} res 
 * @param {*} error
 * @return {*}
 */
function failure(res, error) {
    if (error.name === 'SequelizeValidationError') {
        const errors = error.errors.map(e => e.message);
        return res.status(400).json({
            status: false,
            message: '请求参数错误',
            errors
        });
    }

    if (error.name === 'NotFoundError') {
        return res.status(404).json({
            status: false,
            message: '资源不存在',
            errors: [error.message]
        });
    }

    res.status(500).json({
        status: false,
        message: '服务器错误',
        errors: [error.message]
    });
}

module.exports = {
    NotFoundError,
    success,
    failure
}